﻿using BlockMe.Repository.MySQL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using BlockMe.Model;
using System.Collections.Generic;
using Alpha;
using Alpha.DataBase;
using System.IO;

namespace BlockMe.Repository.MySQL.Test
{
    
    
    /// <summary>
    ///This is a test class for PostRepositoryTest and is intended
    ///to contain all PostRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PostRepositoryTest
    {


        private TestContext testContextInstance;
        private List<string> dropTables = new List<string>();
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestCleanup]
        public void Cleanup()
        {
            for (int i = 0; i < dropTables.Count; i++)
            {
                try
                {
                    ProviderFactory.GetProvider().ExecuteNonQuery("Drop table " + dropTables[i]);
                }
                catch (Exception )
                {
                }
            }
        }
        /// <summary>
        ///A test for PostRepository Constructor
        ///</summary>
        [TestMethod()]
        public void PostRepositoryConstructorTest()
        {
            PostRepository target = new PostRepository();
            Assert.AreEqual("posts", target.PostTableName);
            Assert.AreEqual("comments", target.CommentTableName);
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [TestMethod()]
        public void AddTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            target.PostTableName = "posts" + StringHelper.GetRandomStr(4);
            dropTables.Add(target.PostTableName);
            target.CreatePostTable();
            Post item = new Post {
                Author = "alias",
                AuthorID = 1,
                Content = "This is a test for blockme",
                PublishDate = DateTime.Now,
                Status = PostStatus.Published,
                Title = "1st blog",
            };
            Post actual;
            actual = target.Add(item);
            var reader = ProviderFactory.GetProvider().ExecuteDataReader("select postid,title,authorid,content,publishdate,publishdate_gmt,post_status,author_name,comment_count from " + target.PostTableName);
            Assert.AreEqual(true, reader.HasRows);
            if (reader.Read())
            {
                Assert.AreEqual("1", item.PostID.ToString());
                Assert.AreEqual(item.AuthorID, reader.GetInt32(2));
                Assert.AreEqual(item.Title, reader.GetString(1));
                Assert.AreEqual(item.Content, reader.GetString(3));
                Assert.AreEqual(item.PublishDate.ToString(), reader.GetDateTime(4).ToString());
                Assert.AreEqual(item.PublishDate.ToUniversalTime().ToString(), reader.GetDateTime(5).ToString());
                Assert.AreEqual(item.Status.ToString(), reader.GetString(6));
                Assert.AreEqual(item.Author, reader.GetString(7));
                Assert.AreEqual(0, reader.GetInt32(8));
                reader.Close();
            }

        }

        /// <summary>
        ///A test for Count
        ///</summary>
        [TestMethod()]
        public void CountTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            PostStatus key = new PostStatus(); // TODO: Initialize to an appropriate value
            int expected = 0; // TODO: Initialize to an appropriate value
            int actual;
            actual = target.Count(key);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Counter
        ///</summary>
        [TestMethod()]
        public void CounterTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            int expected = 0; // TODO: Initialize to an appropriate value
            int actual;
            actual = target.Counter();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod()]
        public void DeleteTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            Post item = null; // TODO: Initialize to an appropriate value
            target.Delete(item);
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        /// <summary>
        ///A test for GetAll
        ///</summary>
        [TestMethod()]
        public void GetAllTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            IEnumerable<Post> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<Post> actual;
            actual = target.GetAll();
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetByTitle
        ///</summary>
        [TestMethod()]
        public void GetByTitleTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            string title = string.Empty; // TODO: Initialize to an appropriate value
            Post expected = null; // TODO: Initialize to an appropriate value
            Post actual;
            actual = target.GetByTitle(title);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetPostComment
        ///</summary>
        [TestMethod()]
        public void GetPostCommentTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            int postID = 0; // TODO: Initialize to an appropriate value
            IEnumerable<Comment> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<Comment> actual;
            actual = target.GetPostComment(postID);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetPostsByDate
        ///</summary>
        [TestMethod()]
        public void GetPostsByDateTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            DateTime time = new DateTime(); // TODO: Initialize to an appropriate value
            IEnumerable<Post> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<Post> actual;
            actual = target.GetPostsByDate(time);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetPostsByTag
        ///</summary>
        [TestMethod()]
        public void GetPostsByTagTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            string tag = string.Empty; // TODO: Initialize to an appropriate value
            IEnumerable<Post> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<Post> actual;
            actual = target.GetPostsByTag(tag);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Retrieve
        ///</summary>
        [TestMethod()]
        public void RetrieveTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            Post condition = null; // TODO: Initialize to an appropriate value
            IEnumerable<Post> expected = null; // TODO: Initialize to an appropriate value
            IEnumerable<Post> actual;
            actual = target.Retrieve(condition);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for Update
        ///</summary>
        [TestMethod()]
        public void UpdateTest()
        {
            PostRepository target = new PostRepository(); // TODO: Initialize to an appropriate value
            Post updateEntity = null; // TODO: Initialize to an appropriate value
            Post expected = null; // TODO: Initialize to an appropriate value
            Post actual;
            actual = target.Update(updateEntity);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
    }
}
